#       OpenFeedSourceDialog.py
#       
#       Copyright 2008 Joshua Scotton <josh@joshuascotton.com>
#       
#       Following Methods Copyright (C) 2007 Lauri Taimila
#           * on_url_dialog_delete_event
#           * on_url_dialog_cancel_button_clicked
#           * on_url_dialog_ok_button_clicked
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import string
import ConfigParser
from ConfigParser import ParsingError
from utils.feed_utils import OPMLParser, FeedConfigTools

try:
 	import pygtk
  	pygtk.require("2.4")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

class OpenFeedSourceDialog:
        
    widgets = None
    dialog = None
    tree_widget = None
    feeds = None
    config = None
    url = None
    
    def __init__(self, the_widget, the_feeds, the_config):
        """initialises the gtk window and displays it"""
        
        #feeds is a pointer to a list of feeds from the config file
        self.feeds = the_feeds
        
        #needed so we can add feeds to the feed list widget
        self.tree_widget = the_widget
        
        #it's pointless adding feeds to the widget if we don't save them to the config as well!
        self.config = the_config
        
        # Load glade UI
        self.gladefile = "utils/glade/open_feed_source_dialog.glade"
        self.widgets = gtk.glade.XML(self.gladefile)
        
        # Get content management dialog and bind signal callbacks
        self.dialog = self.widgets.get_widget("open_source_dialog")
        if (self.dialog):
            callback_dic = { 
                "on_fileOpen_clicked" : self.on_fileOpen_clicked,
                "on_lifereaButton_clicked" : self.on_lifereaButton_clicked,
                "on_enterURL_clicked" : self.on_enterURL_clicked,
                "on_closeButton_clicked" : self.on_closeButton_clicked,
                "on_url_dialog_ok_button_clicked" : self.on_url_dialog_ok_button_clicked,
                "on_url_dialog_cancel_button_clicked" : self.on_url_dialog_cancel_button_clicked,
                "on_url_dialog_delete_event" : self.on_url_dialog_delete_event
                 }
        
        self.widgets.signal_autoconnect(callback_dic)
        
        # Initilize dialog widgets with correct values and show dialog
        self.dialog.resize(300,200)
        self.dialog.run()
    
    def on_fileOpen_clicked(self, widget):
        """Opens add file dialog and then adds all feeds in the opml file selected """
        
        #get the model for the feed list widget so we can add the new feeds
        model = self.tree_widget.get_model()
        
        #create select file dialog
        dialog = gtk.FileChooserDialog("Select OPML file",None,gtk.FILE_CHOOSER_ACTION_OPEN,(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))

        #set dialog up to filter for only opml files
        filter = gtk.FileFilter()
        filter.set_name("OPML files")
        filter.add_pattern("*.opml")
        dialog.add_filter(filter)

        #set dialog up to allow multiple selections
        dialog.set_select_multiple(True)
        
        #run the dialog
        status = dialog.run()

        # if file was selected, get list of feeds from it and add to 
        #model and update config file
        if(status == gtk.RESPONSE_OK):
            FeedConfigTools().add_file_feeds_to_widget(dialog.get_filenames(), self.config, model, self.feeds)
        dialog.destroy()
        
    def on_lifereaButton_clicked(self, widget):
        """adds any liferea feeds it finds to the feed widget and config file"""
        #get the model
        model = self.tree_widget.get_model()
        #get the liferea feeds and then send everything to the add_file_feeds_to_widget method
        FeedConfigTools().add_file_feeds_to_widget([OPMLParser().get_liferea_opml()], self.config, model, self.feeds)
        
    def on_enterURL_clicked(self, widget):
        """gets a opml file link from a user and adds any feeds it finds to the feed widget and config file"""
        url_dialog = self.widgets.get_widget("url_dialog")
        model = self.tree_widget.get_model()
        # Open dialog
        url_dialog.set_title("Add OPML File")
        status = url_dialog.run()
        # If folder was selected we add it to model and update config file
        if(status == gtk.RESPONSE_OK):
            FeedConfigTools().add_file_feeds_to_widget([self.url], self.config, model, self.feeds)
        
    def on_closeButton_clicked(self, widget):
        self.dialog.hide()
        self.dialog.destroy()
    
    def on_url_dialog_ok_button_clicked(self, widget):
        """URL dialog OK button pressed. Sets self.url"""
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        url_dialog.hide()
        self.url = url_entry.get_text()
        url_entry.set_text("")
        url_dialog.response(gtk.RESPONSE_OK)
        
    def on_url_dialog_cancel_button_clicked(self, widget):
        """URL dialog cancelled. Hides dialog"""
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        url_dialog.hide()
        url_entry.set_text("")
        url_dialog.response(gtk.RESPONSE_CANCEL)
        
    def on_url_dialog_delete_event(self, widget, data):
        """Dialog's X clicked. Hides dialog"""
        url_dialog = self.widgets.get_widget("url_dialog")
        url_entry = self.widgets.get_widget("url_entry")
        url_dialog.hide()
        url_entry.set_text("")
        url_dialog.response(gtk.RESPONSE_CANCEL)
        return True
    
        
